package com.vmj.test.example.array;

/**
 * Created by yangshoujun on 2017/9/9 下午6:25.
 */
public class BubbleSort {

    public static void main(String[] args) {

        int[] arr = new int[]{90, 80, 70, 60, 50, 40, 30, 20, 10};
        print(arr);
        bubbleSort(arr);
    }

    public static void bubbleSort(int arr[]) {
        boolean swaped = true;
        int j = 0;
        int tmp;
        while (swaped) {
            swaped = false;
            j++;
            for(int i = 0;i < arr.length - j;i++) {
                if(arr[i] > arr[i+1]) {
                    tmp = arr[i];
                    arr[i] = arr[i+1];
                    arr[i+1] = tmp;
                    swaped = true;
                }
            }
            print(arr);
        }
    }

    private static void print(int arr[]) {
        for(int val : arr) {
            System.out.print(val);
            System.out.print('\t');
        }
        System.out.println();
    }
}
